/* typehinst:start */
import { GameRoot } from "../../root";
/* typehinst:end */

import { animRemoveElement, makeDiv } from "../../../core/utils/utils";
import { BaseHUDPart } from "../base_hud_part";

export class HUDShowMessage extends BaseHUDPart{
  /** @type {GameRoot} */
  constructor(root){
    super(root);
  }

  createElements(parent){
    this.element = makeDiv(parent, 'show_message', [], `
      <div class="infos"></div>
    `);
  }
  
  initialize(){
    // this.alert('testestestsetset');
    this.root.hud.signals.showMessage.add(this.message.bind(this));
    // this.root.hud.signals.log.add(this.log.bind(this));

    // this.logsText = [];
  }

  message(type, ...arg){
    this[type](...arg);
  }

  alert(text, delay = 2000){
    let box = makeDiv(this.element, '', ['alert'], `
      ${text}
    `);
    this.log('系统', text, 'log');
    // box.style.animationDuration = delay / 1000 + 's';
    animRemoveElement(box, () => {
      this.element.removeChild(box)
    }, 300, delay)
  }

  info(text){
    let box = makeDiv(this.element.querySelector('.infos'), '', ['info'], `
      ${text}
    `, this.element.querySelectorAll('.info')[0]);
    animRemoveElement(box, () => {
      this.element.querySelector(".infos").removeChild(box);
    }, 300, 2000)
  }

  infos(arr, delay = .3){
    for(let i = 0; i < arr.length; i++){
      let text = arr[i];

      setTimeout(() => {
        this.info(text);
      }, i * delay * 1000);
    }
  }

  /**
   * 
   * @param {*} send 
   * @param {*} text 
   * @param {String} type log/world
   */
  log(send = '系统', text = '', type){
    this.root.hud.parts.logBoard.add({
      send, text, type,
      time: this.root.time.getCurrentDateDesc(),
      date: new Date().getTime()
    })

    let battlePage = this.root.gameState.htmlElement.querySelector('#battle_page');
    if(battlePage){
      battlePage.querySelector('header .left').innerHTML = text;
    } else {
      this.root.gameState.htmlElement.querySelector('header .left').innerHTML = send + ': ' + text;
    }
  }
}